<?php
/**
 * Created by PhpStorm.
 * User: shiyi
 * Date: 2018/6/8
 * Time: 18:09
 */
namespace app\api\controller;
use think\Db;

class Lagou extends Base {

    //职位列表
    public function jobList(){
        $page = input('page', 1);
        $limit = input('limit', 10);
        $data = Db::name('lagou_job')
            ->alias('j')
            ->leftJoin('lagou_company c','j.company = c.lagou_id')
            ->field('j.*,c.name as company_name')
            ->order('j.id asc')
            ->page($page, $limit)
            ->select();
        $count = Db::name('lagou_job')->count();
        return $this->formatData($data, $count);
    }

    //公司列表
    public function companyList(){
        $page = input('page', 1,'int');
        $limit = input('limit', 10,'int');
        $data = Db::name('lagou_company')->page($page, $limit)->select();
        $count = Db::name('lagou_company')->count();
        return $this->formatData($data, $count);
    }

    //公司详情
    public function companyInfo(){
        $id = input('id',0);
        if($id == 0)
            return ['code'=>403,'msg'=>'非法参数'];
        return Db::name('lagou_company')->where(['lagou_id'=>$id])->find();
    }

    //地区-职位柱状图
    public function charts_data(){
        $data = [];
       // $data['area_job'] = Db::name('lagou_job')->query('SELECT area,COUNT(*) as num FROM lagou_job GROUP BY area order by num desc');
//        $area_job = Db::name('lagou_job')
//                            ->fieldRaw('area,COUNT(*) as num')
//                            ->group('area')
//                            ->order('num desc')
//                            ->select();
        $jobs= Db::name('lagou_job')->field('area,tags,education,experience')->where('tags != ""')->select();
        $jobs_tags = [];
        $jobs_educations = [];
        $jobs_areas = [];
        $jobs_experiences = [];
        foreach ($jobs as $v){
            // 标签
            $tag = explode(';',$v['tags']);
            foreach ($tag as $vv){
                array_push($jobs_tags, strtolower($vv));
            }
            // 学历
            array_push($jobs_educations, $v['education']);
            // 地区
            array_push($jobs_areas,$v['area']);
            // 经验
            array_push($jobs_experiences, $v['experience']);
        }
        //经验数组
        $experience_chart = array_count_values($jobs_experiences);
        arsort($experience_chart);
        //地区数组
        $area_chart = array_count_values($jobs_areas);
        arsort($area_chart);
        //学历数组
        $education_chart = array_count_values($jobs_educations);
        arsort($education_chart);
        //标签表格数据
        $jobs_tags = array_count_values($jobs_tags);
        arsort($jobs_tags);
        $tags_chart_legendData = array_keys($jobs_tags);
        $legendData_length = count($tags_chart_legendData);
        $tags_chart_selected = [];
        $tags_chart_seriesData = [];
        for($i=0; $i<$legendData_length;$i++){
            $name = $tags_chart_legendData[$i];
            $tags_chart_selected[$name] = $i<=10;
            $item = ['name'=>$name, 'value'=>$jobs_tags[$name]];
            array_push($tags_chart_seriesData,$item);
        }
        $data = [
            'area_chart'=>[
                'x'=>array_keys($area_chart),
                'y'=>array_values($area_chart),
            ],
            'tags_chart'=>[
                'legendData'=>$tags_chart_legendData,
                'selected'=>$tags_chart_selected,
                'seriesData'=>$tags_chart_seriesData
            ],
            'education_chart'=>[
                'x'=>array_keys($education_chart),
                'y'=>array_values($education_chart)
            ],
            'experience_chart'=>[
                'x'=>array_keys($experience_chart),
                'y'=>array_values($experience_chart)
            ]
        ];
        return $data;
    }
}